<template>
  <div>
    <Drawer
      ref="modal"
      :title="editType == 'update'?$t('companyManagement.edit'):$t('companyManagement.companyAdd')"
      :width="500"
      :value="show"
      :beforeClose="$_modal_decideIfClose"
      @on-close="show=false"
    >
      <Form
        ref="form"
        :labelWidth="160"
        :model="formData"
        :rules="formRule"
      >
        <FormItem prop="name" :label="$t('companyManagement.companyName')">
          <Input style="width: 200px" v-model="formData.name"  :placeholder="$t('companyManagement.pleaseEnter')" />
        </FormItem>
        <FormItem prop="code" :label="$t('companyManagement.enterpriseNumber')">
          <Input  style="width: 200px" v-model="formData.code" :placeholder="$t('companyManagement.pleaseEnter')" />
        </FormItem>
        <FormItem prop="contact" :label="$t('companyManagement.contacts')">
          <Input  style="width: 200px" v-model="formData.contact" :placeholder="$t('companyManagement.pleaseEnter')" />
        </FormItem>
        <FormItem prop="industry" :label="$t('companyManagement.industry')">
          <Input  style="width: 200px" v-model="formData.industry" :placeholder="$t('companyManagement.pleaseEnter')" />
        </FormItem>
        <FormItem class="glo-btn-group">
          <Button
            type="primary"
            :loading="loading"
            @click="handleSubmit"
          >{{ $t("devicesCreate.devCreateSubmit") }}</Button>
          <Button
            @click="$_modal_close"
          >{{ $t("devicesCreate.devCreateCancel") }}</Button>
        </FormItem>
      </Form>
    </Drawer>
  </div>
</template>

<script>
import {
  create,
  update,
  getItem
} from '@/api/company.js'
// import {
//   getList,
// } from '@/api/company.js'
import modalMixin from '@/utils/mixins/modalMixin.js'
//自定义规则校验邮箱输 入
var validateEmail = (rule, value, callback) => {
  
};

export default {
  mixins: [
    modalMixin,
  ],

  props: {
    editType: {
      type: String,
      default: 'create',
    },
    editItem: Object,
  },
  data () {
    return {
      show: false,
      loading: false,
      // roles: [],
      formData: {
        code: '',
        name: '',
        contact: '',
        industry: '',
      },
      formRule: {
        name: { required: true, message: this.$t('emailSetting.Noempty'), trigger: 'blur' },
        // contact: { required: true, message: this.$t('emailSetting.Noempty'), trigger: 'blur' },
        contact: {required: true,validator: (rule, value, callback) => {
          var reg = /^1[3456789]\d{9}$/
          if (this.formData.contact) {
            if (!reg.test(this.formData.contact)) {
              return callback(new Error(this.$t('companyManagement.phoneTip')))
            } else {
              return callback()
            }
          } else {
            return callback(new Error(this.$t('emailSetting.Noempty')))
          }
        }, trigger: 'blur' },
      },
    }
  },
  

  mounted () {
    this.show=true;
    if(this.editType == 'update'){
      this.getData()
    }  
    
  },

  methods: {
    async handleSubmit () {
      const ifVlid = await this.$refs.form.validate()
      if (ifVlid && this.editType === 'update') {
        
        this.handleUpdate()
      }else if(ifVlid && this.editType === 'create'){
        this.handleCreate()
      }
    },
    
    async handleCreate () {
      // const formData = this.generateFormData()
      // console.log(formData)
      const { success, data } = await create(this.formData)
      if (success) {
        this.$Message.success(this.$t('devicesCreate.devCreateS'))
        this.ifUpdateUpper = true
        this.$_modal_close()
        this.$emit('updata')
      } else {
        this.$Message.error(data)
      }
    },
    async handleUpdate () {
      let data2=this.formData;
      data2.id=this.editItem.id;
      const { success, data } = await update(data2)
      if (success) {
        this.$Message.success(this.$t('devicesCreate.devCreateUpdateS'))
        this.$emit('updata')
      } else {
        this.$Message.error(data)
      }
      this.$_modal_close()
    },
    async getData(){
      let res=await getItem({id:this.editItem.id})
      if(res.data){
        this.formData.name=res.data.data.name
        this.formData.code=res.data.data.code
        this.formData.contact=res.data.data.contact
        this.formData.industry=res.data.data.industry
      }else{
        this.$Message.error("查询当前数据失败！")
      }
    },
   
  },
}
</script>
